﻿using MockPrjTeam1.Entity;
using SD.Commons.Helpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace MockPrjTeam1.DAO
{
    public class DistrictDAO
    {
        public List<District> GetActive(int regionId)
        {
            var list = new List<District>();
            IDataReader results;

                if (regionId != 0)
                {
                    var temp = new Collection<SqlParameter>();
                    temp.Add(new SqlParameter("p1", regionId));

                    results = SqlHelper.ExecuteReader("GetActiveRegionDistrict", temp);
                }
                else
                    results = SqlHelper.ExecuteReader("GetActiveDistrict");
                
            while (results.Read())
            {
                var item = new District();
                item.ID = results.GetInt32(0);
                item.Name = results.GetString(1);
                item.Description = results.GetString(2);
                item.Region = results.GetString(6);
                item.Active = results.GetBoolean(4);
                list.Add(item);
            }

            return list;
        }

        public List<District> GetAllRegionDistrict(int regionId)
        {
            var list = new List<District>();

            var temp = new Collection<SqlParameter>();
            temp.Add(new SqlParameter("p1", regionId));

            var results = SqlHelper.ExecuteReader("GetAllRegionDistrict", temp);

            while (results.Read())
            {
                var item = new District();
                item.ID = results.GetInt32(0);
                item.Name = results.GetString(1);
                item.Description = results.GetString(2);
                item.Region = results.GetString(6);
                item.Active = results.GetBoolean(4);
                list.Add(item);
            }

            return list;
        }

        public District GetOne(int districtId)
        {
            var district = new District();

            var temp = new Collection<SqlParameter>();
            temp.Add(new SqlParameter("p1", districtId));

            var results = SqlHelper.ExecuteReader("GetOneDistrict", temp);
            
            if (results.Read())
            {
                district.ID = results.GetInt32(0);
                district.Name = results.GetString(1);
                district.Description = results.GetString(2);
                district.Region = results.GetString(6);
                district.Active = results.GetBoolean(4);
            }
            return district;
        }

        public bool CheckName(string name, int districtId)
        {
            var temp = new Collection<SqlParameter>();
            temp.Add(new SqlParameter("p1", name));
            temp.Add(new SqlParameter("p2", districtId));
            var result = SqlHelper.ExecuteReader("CheckDistrictName", temp);
            if (!result.Read())
                return true;
            else
                return false;
        }

        public bool Edit(District entity)
        {
            var temp1 = new Collection<SqlParameter>();
            temp1.Add(new SqlParameter("p1", entity.Region));
            var result1 = SqlHelper.ExecuteReader("GetRegionID", temp1);
            result1.Read();
            var regionID = result1.GetInt32(0);

            var temp = new Collection<SqlParameter>();
            temp.Add(new SqlParameter("p0", entity.ID));
            temp.Add(new SqlParameter("p1", entity.Name));
            temp.Add(new SqlParameter("p2", entity.Description));
            temp.Add(new SqlParameter("p3", regionID));
            temp.Add(new SqlParameter("p4", entity.Active));
            var result = SqlHelper.ExecuteNonQuery("EditDistrict", temp);
            return true;
        }

        public bool Create(District entity)
        {
            var temp1 = new Collection<SqlParameter>();
            temp1.Add(new SqlParameter("p1", entity.Region));
            var result1 = SqlHelper.ExecuteReader("GetRegionID", temp1);
            result1.Read();
            var regionID = result1.GetInt32(0);

            var temp = new Collection<SqlParameter>();
            temp.Add(new SqlParameter("p1", entity.Name));
            temp.Add(new SqlParameter("p2", entity.Description));
            temp.Add(new SqlParameter("p3", regionID));
            temp.Add(new SqlParameter("p4", entity.Active));
            var result = SqlHelper.ExecuteNonQuery("CreateDistrict", temp);
            return true;
        }

        public List<District> Filter(int regionId, int filterId, int isActive)
        {
            var list = new List<District>();

            var temp = new Collection<SqlParameter>();
            temp.Add(new SqlParameter("p0", regionId));
            temp.Add(new SqlParameter("p1", filterId));
            temp.Add(new SqlParameter("p2", isActive));

            var results = SqlHelper.ExecuteReader("FilterDistrict", temp);

            while (results.Read())
            {
                var item = new District();
                item.ID = results.GetInt32(0);
                item.Name = results.GetString(1);
                item.Description = results.GetString(2);
                item.Region = results.GetString(6);
                item.Active = results.GetBoolean(4);
                list.Add(item);
            }

            return list;
        }
    }
}